package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayApply;
import org.itboys.mobile.entity.mongo.SundayExpert;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/8/14
 * @Description：专家
 */
@Service
public class SundayExpertService extends BaseMongoService<SundayExpert>{
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }

    @Override
    protected Class<SundayExpert> getEntityClass() {
        return SundayExpert.class;
    }

    @Autowired
    private SundayUploadService uploadFileService;

    public PageResult<SundayExpert> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name");
        return super.containpageQuery(request, vagueParam, param);
    }
    public void save(Long id,String name, MultipartFile imageFile) throws IOException {
        SundayExpert expert=new SundayExpert();
        expert.setName(name);
        Map<String, String> map = uploadFileService.uploadSingle(imageFile);
        if (StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))) {
            expert.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,expert);
        }else {
            super.save(expert);
        }
    }

    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/export/choose/choose_erro";
        }
        return "/sunday/export/choose/choose_single";
    }
}
